package models;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import play.db.jpa.Model;

/*
 * A class that represents the ternary relation User_Role_Project
 */

@Entity
@Table(name = "Projects_Roles_Users", uniqueConstraints = { @UniqueConstraint(columnNames = {
		"project_id", "role_id", "user_id" }) })
/*
 * The unique constraint specify that the tuple (user_id,role_id,project_id) is
 * unique "it is a candidate key"
 */
public class UserRoleProject extends Model {
	// Attribute on the relation
	public boolean status;

	public boolean isNotified;

	@ManyToOne
	@JoinColumn(name = "user_id")
	// rename the column name in order to know the column
	// name"which is 'user_id' now" to add it in the constraint
	// list above ,as the constraint list needs the name of the COLUMNS, not the
	// name of the variables representing
	// these columns "user is the name of the variable in our case"
	public User user;

	@ManyToOne
	@JoinColumn(name = "role_id")
	public Role role;

	@ManyToOne
	@JoinColumn(name = "project_id")
	public Project project;

	/**
	 * constructs a new UserRoleProject
	 * 
	 * @param status
	 * @param user
	 * @param role
	 * @param project
	 */
	public UserRoleProject(boolean status, User user, Role role, Project project) {
		super();
		this.status = status;
		this.user = user;
		this.role = role;
		this.project = project;
	}

	/**
	 * constructs a new UserRoleProject
	 * 
	 * @param status
	 * @param role
	 * @param project
	 */
	public UserRoleProject(boolean status, Role role, Project project) {
		super();
		this.status = status;
		this.role = role;
		this.project = project;
	}

	/**
	 * Defines the String representation of the UserRoleProject
	 * 
	 */
	public String toString() {
		String userName = (user == null ? "None" : user.userName);
		String roleTitle = (role == null ? "None" : role.title);
		String projectName = (project == null ? "None" : project.name);
		String stat = (status ? "Approved" : "Pending");
		return "(" + userName + "-" + roleTitle + "-" + projectName + "-"
				+ stat + ")";
	}
}
